<?php

namespace app\admin\controller;
use \think\Controller;
use app\admin\model\Article as articleModel;
use think\Loader;

class Article extends Common
{

//    前置操作,执行del方法之前先执行delsoncate
    protected $beforeActionList = [
        'delsoncate'  =>  ['only'=>'del'],
    ];


//    文章列表
    public function lst()
    {
//        联表查询，过滤哪些字段，起别名，连接条件
        $adminres = db('article')->field('a.*,c.catename')->alias('a')->join('cate c','a.cate_id=c.id')->paginate(10);
//        dump($adminres);die;
        $this->assign('adminres',$adminres);
        return view();
    }

    //    添加文章
    public function add()
    {

        $haha = new articleModel();
        $data = input('post.');


        if(request()->isPost()){
 //  var_dump($_POST);die;
//        tp数据验证
            $data['time'] = time();
//            dump($data);die;
            $validate = Loader::validate('Article');
            if(!$validate->check($data)){
                $this->error($validate->getError());
            }
//            dump($data);die;
//            添加之前上传图片

            if ($_FILES['thumb']['tmp_name']){
                $file = request()->file('thumb');
                $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
                if ($info){
                    $thumb = 'http://shop.heguozhen.cn/'  . DS . 'uploads'.'/'.$info->getSaveName();;
                    $data['thumb'] = $thumb;
                }
            }

//            $res = db('article')->insert(input('post.'));这种方法存不进数据缩略图
            if ($haha->save($data)){
//                如果有值的话就弹框，并且跳到列表页
                $this->success('文章添加成功',url('article/lst'));

            }else{
                $this->error('添加文章失败');
            }
            return;
        }
//        获得用户数据并且发送到前台模板

        $articleres = $haha->getarticle();
        $this->assign('articleres',$articleres);
        return view('add');
    }

//    文章编辑
    public function edit()
    {
        if (request()->isPost()){
//            dump(123);die;
//            $save = db('article')->update(input('post.'));

            $article = new \app\admin\model\Article();
            $save = $article->update(input('post.'));
            if ($save !==false){
                $this->success('修改成功',url('lst'));
            }else{
                $this->error('修改失败');
            }
            return;
        }

//        $articleres =db('article')->select();
//        $this->assign('articleres',$articleres);
        $haha = new articleModel();
        $articleres = $haha->getarticle();
        $arts = db('article')->where(array('id'=>input('id')))->find();
        $this->assign(array(
                'articleres'=>$articleres,
                'arts'=>$arts
        ));
        return view();
    }


    public function del()
    {
        $del = db('article')->delete(input('id'));
        if ($del){
            $this->success('删除栏目成功',url('lst'));
        }else{
            $this->error('删除栏目失败');
        }
    }

    public function delsoncate(){
        $cateid = input('id');
        $cate = new \app\admin\model\Cate();
        $sonid = $cate->getchilredid($cateid);
        $allcateid = $sonid;
        $allcateid[] = $cateid;
        foreach ($allcateid as $k=>$v){
            $article = new \app\admin\model\Article();
           $article->where(array('cate_id'=>$v))->delete();
        }
        if ($sonid){
            db(cate)->delete($sonid);
        }
    }
}
